package com.zq.designpaterns.other.dataaccessobject;

import java.util.ArrayList;
import java.util.List;

/**
 * ================================================
 * 作    者：程志强
 * 邮    箱：cheng.zhiqiang@cesgroup.com.cn
 * 版    本：1.0.0
 * 创建日期：2021/01/27  17:49
 * 修改日期：
 * 描    述：
 * 修订历史：
 * ================================================
 */
public class StudentDaoImpl implements StudentDao {
    // 学生列表
    public List<Student> students;

    public StudentDaoImpl() {
        this.students = new ArrayList<>();
        students.add(new Student("jack", "001"));
        students.add(new Student("json", "002"));
    }

    @Override
    public List<Student> getAllStudents() {
        return students;
    }

    @Override
    public Student getStudent(String rooNo) {
        for (Student student : students) {
            if (rooNo.equalsIgnoreCase(student.getRollNo())) {
                return student;
            }
        }
        return null;
    }

    @Override
    public void updateStudent(Student student) {
        getStudent(student.getRollNo()).setName(student.getName());
        System.out.println("Student: RollNo " + student.getRollNo()
                +", updated in the database");
    }

    @Override
    public void deleteStudent(Student student) {
        students.remove(student);
        System.out.println("Student: Roll No " + student.getRollNo()
                +", deleted from database");
    }
}
